import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/common_widget.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/im/pages/conversation_page.dart';
import 'package:find_friend/pages/mine/mine_xspace.dart';
import 'package:find_friend/pages/xspace/detail/xdetailpage.dart';
import 'package:find_friend/pages/xspace/model/school_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'xall_cell_image.dart';
import 'xalldiscuss.dart';
import 'xcommbar.dart';


class XallSchoolCell extends StatelessWidget {

  final ArticleModel model;
  final bool isself;
  final Function() onRefreshData;
  final Function(XCommentAction action, ArticleModel model,) onTapComment;

  const XallSchoolCell({Key key, this.model, this.onTapComment, this.isself = false, this.onRefreshData}) : super(key: key);

  PopupMenuItem<int> _createMenuButton(String title, int value) {
    return PopupMenuItem(
      height: 30,
      value: value,
      child: Container(
        alignment: Alignment.center,
        child: Text(title, style: instanceTextStyle(14, APP_font_black),
      ),),
    );
  }

  void _selectMenuItem(BuildContext context,int value) {
    switch (value) {
      case 0:{
        Map arg = {"coversationType":1,"targetId":model.mobile, "nickname":model.followRemark};
        push(context, ConversationPage(arguments: arg));
      }
        break;
      case 1:
        _request();
        break;
      case 2:
        Navigator.pushNamed(context, "/msgreport", arguments: model.mobile);
        break;
      default:
    }
  }

  // 关注
  _request() {
    Networktool.request(API.follow, params: {model.followflag != null ? "mobiles" : "mobile" : model.mobile}, method: model.followflag != null ? NETWORKHTTPMethod.DELETE : NETWORKHTTPMethod.POST, success: (data){
      AlertTool.showToast(model.followflag != null ? "取消关成功" : "关注成功");
      model.followflag == null ? model.followflag = 1 : model.followflag = null;
    },
    fail: (msg) => AlertTool.showToast(msg));
  }

  @override
  Widget build(BuildContext context) {
    
    final size = MediaQuery.of(context).size;
    return Container(
      margin: const EdgeInsets.only(left: 10,right: 10, top: 10),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color:APP_divider_color, width: 0.5),
        borderRadius: BorderRadius.circular(5)
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.only(left:10.0, top: 10, bottom: 10, right: 5),
            child: Row(
              children: [
                InkWell(
                  onTap: (){
                    Navigator.of(context).pushNamed("/minepreview", arguments: model.mobile);
                  },
                  child: CustomCircleAvatar(url: model?.avatar, size: 46.h,)
                ),
                SizedBox(width: 8,),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(model.remarkname, style: instanceTextStyle(13, APP_font_black),),
                          Offstage(
                            offstage: isself,
                            child: PopupMenuButton(
                              child: Container(
                                width: 40,
                                alignment: Alignment.centerRight,
                                child: Icon(Icons.more_vert, color: APP_divider_color, size: 20,)
                              ),
                              onSelected: (value) => _selectMenuItem(context, value),
                              padding: const EdgeInsets.all(0),
                              itemBuilder: (context) {
                                return <PopupMenuEntry<int>>[
                                  _createMenuButton("私聊", 0),
                                  _createMenuButton(model.followflag != null ? "取消关注" : "关注", 1),
                                  _createMenuButton("举报", 2),
                                  PopupMenuItem(
                                    height: 0.5,
                                    child: Container(height: 0.5, color: APP_divider_color,),
                                  ),
                                  _createMenuButton("取消", 3),
                                ];
                              },
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 4,),
                      Padding(
                        padding: const EdgeInsets.only(right: 30.0),
                        child: Text("个性签名：${model.bio ?? "这个人很懒什么都没有留！"}", maxLines: 1, overflow: TextOverflow.ellipsis, style: instanceTextStyle(9, APP_font_grey),),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
          
          // 文本消息
          Offstage(
            offstage: model.content == null,
            child: Padding(
              padding: const EdgeInsets.only(left:10.0),
              child: Text(model.content ?? "", style: instanceTextStyle(12, APP_font_black),),
            ),
          ),

          // 图片
          Offstage(
            offstage: model.photo == null,
            child: Container(
              padding: const EdgeInsets.only(left: 10, top: 10),
              child: XallImageWidget(list: model.photo ?? [],)
            ),
          ),

          // 语音
          Offstage(
            offstage: model.voice == null,
            child: Padding(
              padding: const EdgeInsets.only(top:10.0, left: 10),
              child: XspaceVoiceView(width: size.width * 0.45, voice: model.voice,),
            ),
          ),
          
          // 热门评论
          Offstage(
            offstage: model.hot == null,
            child: Padding(
              padding: const EdgeInsets.only(top:10.0),
              child: XAllDiscussWidget(model: model?.hot,),
            ),
          ),

          // comment
          InkWell(
            onTap: (){}, // 禁止点击点赞区域跳入详情
            child: XpageCommentWidget(model: model, onTapComment: () {
              push(context, XDetailPage(model: model));
            },)
          )
        ],
      ),
    );
  }
}